import usbManager from '@ohos.usbManager';
import router from '@system.router';

export default {
    data: {
        currentFuncs: null,
        funcString: "",
        funcCode: 0,
        resultSet: true,
    },
    onInit() {
        this.getFunctions();
    },
    setFunction(funType) {
        console.info("usb setCurrentFunctions begin----")
        usbManager.setCurrentFunctions(funType).then(data => {
            console.info("usb setCurrentFunctions : " + data);
            this.resultSet = data;
            this.getFunctions();
        }).catch(error => {
            console.info("usb setCurrentFunctions error : " + error);
            this.resultSet = error;
            this.getFunctions();
        });
    },
    functionStrToNum(){
        try {
            this.funcCode = usbManager.usbFunctionsFromString(this.funcString);
        } catch (err) {
            console.info('usb usbFunctionsFromString catch err code: ' + err.code + ' message: ' + err.message);
            this.funcCode = 'usb usbFunctionsFromString catch err code: ' + err.code + ' message: ' + err.message;
        }
    },

    getFunctions() {
        console.info("usb getCurrentFunctions begin----");
        var funcCode = usbManager.getCurrentFunctions();
        this.funcString = usbManager.usbFunctionsToString(funcCode);
        this.currentFuncs = "function字符串形式:" + this.funcString + " \nfunction掩码形式: " + funcCode;
        console.info("usb getCurrentFunctions : " + this.currentFuncs);
    },
    onBackPress() {
        router.back();
    }
}
